<?php
//
//	file: includes/acp/acp_attach_ext.php
//	author: ptirhiik
//	begin: 24/02/2006
//	version: 1.6.2 - 23/08/2007
//	license: http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
//
//	based on : attachment_mod copyright (c) 2002 Meik Sievertsen (acydburn)
//

if ( !defined('IN_PHPBB') )
{
	die('Hack attempt');
}

$cp_title = 'Manage_extensions';
$cp_title_explain = 'Manage_extensions_explain';

// we need some functions
include_once($config->url('includes/attach/class_attach_cp_fields'));

class attach_manage_ext extends form
{
	var $data;

	function attach_manage_ext($requester, $parms='', &$fields)
	{
		$this->requester = $requester;
		$this->parms = empty($parms) ? array() : $parms;

		$this->data = array();
		parent::form($fields);
	}

	function process()
	{
		if ( $this->init() )
		{
			$this->check();
			$this->validate();
			$this->display();
			return true;
		}
		return false;
	}

	function init()
	{
		global $db;

		parent::init();
		$this->read();
		return true;
	}

	function read($force=false)
	{
		global $db, $config;

		// get extensions
		$this->data = array();
		$sql = 'SELECT e.ext_id, e.extension, e.ext_comment, e.ext_group_id, g.cat_id
					FROM ' . ATT_EXTENSIONS_TABLE . ' e
						LEFT JOIN ' . ATT_EXT_GROUPS_TABLE . ' g
							ON g.ext_group_id = e.ext_group_id
					ORDER BY g.cat_id, e.ext_group_id, e.extension';
		$result = $db->sql_query($sql, false, __LINE__, __FILE__);
		while ( $row = $db->sql_fetchrow($result) )
		{
			$this->data[ intval($row['ext_id']) ] = array(
				'extension' => $row['extension'],
				'ext_comment' => $row['ext_comment'],
				'cat_id' => intval($row['cat_id']),
				'ext_group_id' => intval($row['ext_group_id']),
			);
		}
		$db->sql_freeresult($result);
		if ( $force )
		{
			$config->globals['mod_attachment_CH']['extensions']->read(true);
		}
	}

	function check()
	{
		global $config, $db;
		global $error, $error_msg;
		global $warning, $warning_msg;

		if ( $error || $warning || !_button('add_new') )
		{
			return;
		}
		parent::check();
	}

	function validate()
	{
		global $config, $db;
		global $error, $error_msg;
		global $warning, $warning_msg;

		$deleted_id = _read('delete', TYPE_INT, 0, array(0 => '') + $this->data);
		if ( $error || $warning || (!_button('add_new') && !_button('edit_details') && !$deleted_id) )
		{
			return;
		}

		// process actions
		$deleted = $updated = $created = false;
		if ( $deleted_id )
		{
			$sql = 'DELETE FROM ' . ATT_EXTENSIONS_TABLE . '
						WHERE ext_id = ' . intval($deleted_id);
			$db->sql_query($sql, false, __LINE__, __FILE__);
			unset($this->data[$deleted_id]);
			$deleted = true;
		}
		else if ( _button('edit_details') )
		{
			$ext_ids = get_var('ext_ids', array(0));
			if ( !empty($ext_ids)  && !empty($this->data) )
			{
				$ext_ids = empty($ext_ids) ? array() : array_flip($ext_ids);
				$ext_comments = get_var('comments_list', array(''));
				$ext_group_ids = get_var('groups_list', array(0));
				foreach ( $this->data as $ext_id => $data )
				{
					if ( isset($ext_ids[$ext_id]) )
					{
						$idx = $ext_ids[$ext_id];
						$ext_comment = trim(htmlspecialchars(stripslashes($ext_comments[$idx])));
						$ext_group_id = intval($ext_group_ids[$idx]);
						$fields = array();
						if ( $ext_comment != $data['ext_comment'] )
						{
							$fields['ext_comment'] = $ext_comment;
							$this->data[$ext_id]['ext_comment'] = $ext_comment;
						}
						if ( $ext_group_id != $data['ext_group_id'] )
						{
							$fields['ext_group_id'] = $ext_group_id;
							$this->data[$ext_id]['ext_group_id'] = $ext_group_id;
						}
						if ( !empty($fields) )
						{
							$updated = true;
							$sql = 'UPDATE ' . ATT_EXTENSIONS_TABLE . '
										SET ' . $db->sql_fields('update', $fields) . '
										WHERE ext_id = ' . intval($ext_id);
							$db->sql_query($sql, false, __LINE__, __FILE__);
						}
					}
				}
			}
		}
		else if ( _button('add_new') )
		{
			$extension = trim(strtolower($this->fields['extension']->value));
			$ext_comment = $this->fields['ext_comment']->value;
			$ext_group_id = $this->fields['ext_group_id']->value;
			if ( !empty($extension) )
			{
				// check if already exists
				$sql = 'SELECT ext_id
							FROM ' . ATT_EXTENSIONS_TABLE . '
							WHERE extension = \'' . $db->sql_escape_string($extension) . '\'
							LIMIT 1';
				$result = $db->sql_query($sql, false, __LINE__, __FILE__);
				$found = $db->sql_numrows($result);
				$db->sql_freeresult($result);
				if ( !$found )
				{
					$fields = array(
						'extension' => $extension,
						'ext_comment' => $ext_comment,
						'ext_group_id' => $ext_group_id,
					);
					$sql = 'INSERT INTO ' . ATT_EXTENSIONS_TABLE . '
								(' . $db->sql_fields('fields', $fields) . ') VALUES(' . $db->sql_fields('values', $fields) . ')';
					$db->sql_query($sql, false, __LINE__, __FILE__);
					$created = true;
				}
			}
		}
		if ( $deleted || $updated || $created )
		{
			$this->read(true);
			$return_msg = $created ? 'Extension_added' : ($deleted ? 'Extension_deleted' : 'Extension_updated');
			message_return($return_msg, 'Click_return_attach_config', $config->url($this->requester, $this->parms, true));
		}
	}

	function display()
	{
		global $template, $config, $user;

		// upper form
		$this->buttons = array(
			'add_new' => array('txt' => 'Add_new', 'img' => 'cmd_add', 'key' => 'cmd_add'),
		);
		parent::display('ADD_FORM');

		// list
		if ( !empty($this->data) )
		{
			$categories = get_special_categories();
			$groups = get_extension_groups_list();

			$color = false;
			$previous_cat_id = -1;
			foreach ( $this->data as $ext_id => $row )
			{
				if ( $row['cat_id'] != $previous_cat_id )
				{
					$color = false;
				}
				$color = !$color;
				$template->assign_block_vars('row', array(
					'EXT_ID' => $ext_id,
					'EXTENSION' => $row['extension'],
					'COMMENT' => empty($row['ext_comment']) ? '' : _html_entities_encode($row['ext_comment']),
					'U_DELETE' => $config->url($this->requester, $this->parms + array('delete' => $ext_id), true),
				));
				foreach ( $groups as $ext_group_id => $ext_group_name )
				{
					$template->assign_block_vars('row.option', array(
						'GROUP_ID' => $ext_group_id,
						'GROUP_NAME' => $ext_group_name,
					));
					$template->set_switch('row.option.selected', $row['ext_group_id'] == $ext_group_id);
				}
				$template->set_switch('row.light', $color);
				if ( intval($row['cat_id']) != $previous_cat_id )
				{
					$previous_cat_id = $row['cat_id'];
					$template->assign_block_vars('row.cat', array(
						'L_CATEGORY' => $user->lang($categories[ $row['cat_id'] ]),
					));
				}
			}
		}
		$template->assign_vars(array(
			'L_EXTENSION' => $user->lang('Extension'),
			'L_COMMENT' => $user->lang('Explanation'),
			'L_GROUP' => $user->lang('Extension_group'),
			'L_ACTION' => $user->lang('Action'),
			'L_NO_EXTENSION' => $user->lang('No_extensions'),

			'I_DELETE' => $user->img('cmd_delete'),
			'L_DELETE' => $user->lang('Delete'),
		));
		$template->set_switch('details');

		// list
		display_buttons(array(
			'edit_details' => array('txt' => 'Edit', 'img' => 'cmd_edit', 'key' => 'cmd_edit'),
		), 'details');
		$template->assign_block_vars('cp_content', array('BOX' => $template->include_file('acp/acp_attach_ext_box.tpl', 'row')));
	}
}

// process
$parms = array(
	'mode' => $menu_id,
	'sub' => $subm_id == $menu_id ? '' : $subm_id,
	'ctx' => $ctx_id == $subm_id ? '' : $ctx_id,
);
$form = new attach_manage_ext($cp_requester, $cp_parms + $parms, $fields);
$form->process();
$template->set_filenames(array('body' => 'cp_generic.tpl'));

?>